package com.gzk.basic.controller;

import com.gzk.basic.util.AjaxResult;
import com.gzk.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {


    @PostMapping
    public AjaxResult upload(@RequestPart(required = true,value = "file") MultipartFile file) throws IOException {
        try {
            String filename = file.getOriginalFilename();
            String extName = filename.substring(filename.lastIndexOf(".")+1);
            String path = FastDfsUtil.upload(file.getBytes(), extName);
            return AjaxResult.ok().setSuccess(true).setMessage("上传成功！").setResultObj(path);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ok().setSuccess(false).setMessage("上传失败！！");
        }
    }

    @DeleteMapping
    public AjaxResult delete(@RequestParam("path") String path){
        try {
            String s = path.substring(path.indexOf("/") + 1);
            String groupName = s.substring(0, s.indexOf("/"));
            String filename = s.substring(s.indexOf("/") + 1);
            FastDfsUtil.delete(groupName, filename);
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ok().setSuccess(false).setMessage("删除失败！！");
        }

    }

}
